package main

import (
	"bytes"
	"io/ioutil"
	"os"
	"strconv"

	"git.oschina.net/yangdao/extlib/tools"
)

func main() {
	data := tools.ScanProtoBuffFile("PBMessage.proto")

	{
		//生成PBMessageID.go
		var OurFileStr = `package ` + data.PkgName + " \r\n"
		OurFileStr += "const ( \r\n"
		for i := 0; i < len(data.Messages); i++ {
			OurFileStr += `PBID_` + data.Messages[i].Name + ` = ` + strconv.Itoa(i) + "\r\n"
		}
		OurFileStr += ")"

		ioutil.WriteFile("PBMessageID.go", bytes.NewBufferString(OurFileStr).Bytes(), os.ModeAppend)
	}
	{
		//生成IDToMessageMap.go
		var OurFileStr = `package ` + data.PkgName + " \r\n"
		OurFileStr += `import "github.com/golang/protobuf/proto" ` + "\r\n"
		OurFileStr += `//GetPBMessageByID 根据ID获取PBMessageB结构体` + " \r\n"
		OurFileStr += `func GetPBMessageByID(msgID int32)  proto.Message {` + " \r\n"
		OurFileStr += `switch msgID {` + " \r\n"

		for i := 0; i < len(data.Messages); i++ {
			OurFileStr += `  case ` + strconv.Itoa(i) + ":\r\n"
			OurFileStr += `  return  &` + data.Messages[i].Name + "{} \r\n"
		}

		OurFileStr += ` }` + " \r\n"
		OurFileStr += ` return nil` + " \r\n"
		OurFileStr += ` }` + " \r\n"
		ioutil.WriteFile("IDToMessageMap.go", bytes.NewBufferString(OurFileStr).Bytes(), os.ModeAppend)
	}
	//生成IDToPBType.cs
	{
		var OurFileStr = `using PBMessage;
using System.Collections.Generic;
using System;
public static class IDToPBType
{
  public static Dictionary<int, Type> TypeDic = new Dictionary<int, Type>()
  {`
		for i := 0; i < len(data.Messages); i++ {
			OurFileStr += ` { ` + strconv.Itoa(i) + ` , typeof(` + data.Messages[i].Name + `) },` + "\r\n"
		}
		OurFileStr += ` };
}`

		OurFileStr += `public enum PBMesasgeID
{
`
		for i := 0; i < len(data.Messages); i++ {
			OurFileStr += data.Messages[i].Name + " = " + strconv.Itoa(i) + ",\r\n"
		}
		OurFileStr += `}`
		ioutil.WriteFile("IDToPBType.cs", bytes.NewBufferString(OurFileStr).Bytes(), os.ModeAppend)
	}
}
